﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using StockholmsStad.Data.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;

namespace StockholmsStad.Business.Test
{
    /// <summary>
    /// Summary description for TestUnitTicket
    /// </summary>
    [TestClass]
    public class TestUnitCeremony
    {
        public const string LANGUAGE = "Franska";
        private MockAgentFactory _agentFactory;
        public CeremonyBusiness _ceremonyBusiness;
        public MaintenanceBusiness _maintBusiness;
        private IDataContext _context;
        public User _currentUser;
        private MockRepository _mocks;

        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _mocks = new MockRepository();
            _currentUser = new User();
            _context = _mocks.DynamicMock<IDataContext>();
            _ceremonyBusiness = new CeremonyBusiness(_currentUser);
            IContextFactory contextFactory = new MockContextFactory(_context);
            //_ceremonyBusiness.ContextFactory = contextFactory;
            _agentFactory = new MockAgentFactory();
            _ceremonyBusiness.AgentFactory = _agentFactory;
        }

        [TestMethod]
        public void RetrieveCeremonyLanguages()
        {
            var agent = _mocks.StrictMock<ILanguageAgent>();
            _agentFactory.LanguageAgent = agent;
            Expect.Call(agent.GetCeremonyLanguages(_context)).Return(new List<ValueIdPair>()
                                                                                 {
                                                                                     new ValueIdPair()
                                                                                         {Id = 1, Name = "Svenska"}
                                                                                 });
            _mocks.ReplayAll();
            List<ValueIdPair> origNumOfLang = _ceremonyBusiness.GetCeremonyLanguages();
            Assert.AreEqual(1, origNumOfLang[0].Id);
            _mocks.VerifyAll();
        }

        [TestMethod]
        public void InsertCeremonyLanguage()
        {
            ILanguageAgent languageAgent = _mocks.StrictMock<ILanguageAgent>();
            _agentFactory.LanguageAgent = languageAgent;
            Expect.Call(() => languageAgent.AddCeremonyLanguage(_context, "Finska"));
            _mocks.ReplayAll();
            _maintBusiness.AddCeremonyLanguage("Finska");
            _mocks.VerifyAll();
        }

        [TestMethod]
        public void RetrieveCertificateLanguages()
        {
            var languageAgent = _mocks.StrictMock<ILanguageAgent>();
            _agentFactory.LanguageAgent = languageAgent;
            Expect.Call(languageAgent.GetCertificateLanguages(_context)).Return(new List<ValueIdPair>()
                                                                                    {
                                                                                        new ValueIdPair()
                                                                                            {Id = 1, Name = "Svenska"}
                                                                                    });
            _mocks.ReplayAll();
            IList<ValueIdPair> origNumOfLang = _ceremonyBusiness.GetCertificateLanguages();
            Assert.AreEqual(1, origNumOfLang[0].Id);
            _mocks.VerifyAll();
        }

        [TestMethod]
        public void RetrieveCeremonyTypes()
        {
            List<ValueIdPair> list = _ceremonyBusiness.GetCeremonyTypes();
            Assert.AreEqual((int) CeremonyType.Ordinary, list[0].Id);
            Assert.AreEqual(CeremonyType.Ordinary.ToString(), list[0].Name);
            Assert.AreEqual((int) CeremonyType.Individual, list[1].Id);
            Assert.AreEqual(CeremonyType.Individual.ToString(), list[1].Name);
        }

        [TestMethod]
        public void InsertCertificateLanguage()
        {
            var agent = _mocks.StrictMock<ILanguageAgent>();
            _agentFactory.LanguageAgent = agent;
            Expect.Call(agent.AddCertificateLanguage(_context, "Finska")).Return(1);
            _mocks.ReplayAll();
            _maintBusiness.AddCertificateLanguage("Finska");
            _mocks.VerifyAll();
        }

        [TestMethod]
        [ExpectedException(typeof (NotImplementedException))]
        public void RemoveCeremonyLanguage()
        {
            //var origNumOfLang = cb.GetCeremonyLanguages().Count;
            _maintBusiness.DeleteCeremonyLanguage(new ValueIdPair());
            // TODO: hämta antal språk igen och räkna
        }

        [TestMethod]
        [ExpectedException(typeof (NotImplementedException))]
        public void RemoveCertificateLanguages()
        {
            //var origNumOfLang = cb.GetCertificateLanguages().Count;
            _maintBusiness.DeleteCertificateLanguage(new ValueIdPair());
            // TODO: hämta antal språk igen och räkna
        }
    }
}